﻿#include "WaringInfo.h"
#include "ui_WaringInfo.h"

WaringInfo::WaringInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WaringInfo)
{
    ui->setupUi(this);
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    errorModel = new QSqlQueryModel(this);
    eMapper = new QDataWidgetMapper(this);
    eMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    eMapper->setModel(errorModel);
    connect(eMapper, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndex(int)));

    SlotConnect();
    connect(stackLayout,SIGNAL(currentChanged(int)),this,SLOT(ErrorDataShow(int)));
}

void WaringInfo::ErrorDataShow(int current)
{
    if(current == warningInfoPage)
    {
        onDataBaseInit();
        getErrorInfo();
    }
}

void WaringInfo::onCurrentIndex(int index)
{
    QModelIndex m_index = errorModel->index(index, 0);
    QModelIndex n_index = errorModel->index(index, 1);
    QModelIndex s_index = errorModel->index(index, 2);
    ui->m_labelWarnningNum0->setText(errorModel->data(m_index, Qt::DisplayRole).toString());
    ui->m_labelHappenTime0->setText(errorModel->data(n_index, Qt::DisplayRole).toString().insert(4,"-").insert(7,"-").insert(10, " ").insert(13, ":").insert(16,":") );
    ui->m_labelDetailEvent0->setText(errorModel->data(s_index, Qt::DisplayRole).toString());

    index = index + 1;

    QModelIndex x_index = errorModel->index(index, 0);
    QModelIndex y_index = errorModel->index(index, 1);
    QModelIndex z_index = errorModel->index(index, 2);
    ui->m_labelWarnningNum1->setText( errorModel->data(x_index, Qt::DisplayRole).toString() ) ;
    ui->m_labelHappenTime1->setText( errorModel->data(y_index, Qt::DisplayRole).toString().insert(4,"-").insert(7,"-").insert(10, " ").insert(13, ":").insert(16,":") );
    ui->m_labelDetailEvent1->setText( errorModel->data(z_index, Qt::DisplayRole).toString() );

    index = index + 1;

    QModelIndex a_index = errorModel->index(index, 0);
    QModelIndex b_index = errorModel->index(index, 1);
    QModelIndex c_index = errorModel->index(index, 2);
    ui->m_labelWarnningNum2->setText( errorModel->data(a_index, Qt::DisplayRole).toString() ) ;
    ui->m_labelHappenTime2->setText( errorModel->data(b_index, Qt::DisplayRole).toString().insert(4,"-").insert(7,"-").insert(10, " ").insert(13, ":").insert(16,":") );
    ui->m_labelDetailEvent2->setText( errorModel->data(c_index, Qt::DisplayRole).toString() );
}
void WaringInfo::onDataBaseInit()
{
    QString dataBasePath = "/usr/local/sanway/dbTest/tcuerror.db";
    QFile file(dataBasePath);
    if( file.exists() )
    {
        QSqlDatabase errorDb ;
        if( QSqlDatabase::contains("qt_sql_default_connection") )
        {
            errorDb = QSqlDatabase::database("qt_sql_default_connection");
        }
        else
        {
            errorDb = QSqlDatabase::addDatabase("QSQLITE");
        }

        errorDb.setDatabaseName(dataBasePath);
        if( !errorDb.open() )
        {
            return;
        }
        errorModel->setQuery("select * from tagError order by time desc");
        errorDb.close();
    }
    else
    {
        printf("故障信息数据库不存在!\n");
    }
}

void WaringInfo::getErrorInfo()
{
    eMapper->toFirst();
    onCurrentIndex(0);
}


void WaringInfo::SlotConnect()
{
    connect(ui->buttonDetailContent0, SIGNAL(clicked()), this, SLOT(on_buttonDetail_clicked()));
    connect(ui->buttonDetailContent1, SIGNAL(clicked()), this, SLOT(on_buttonDetail_clicked()));
    connect(ui->buttonDetailContent2, SIGNAL(clicked()), this, SLOT(on_buttonDetail_clicked()));
}

void WaringInfo::on_buttonDetail_clicked()
{
    QPushButton* btn =  static_cast<QPushButton*>(sender());

    if(btn == ui->buttonDetailContent0)
    {
        emit Check_Alert_Details(ui->m_labelDetailEvent0->text().trimmed().toInt());
    }
    else if(btn == ui->buttonDetailContent1)
    {
        emit Check_Alert_Details(ui->m_labelDetailEvent1->text().trimmed().toInt());
    }
    else if(btn == ui->buttonDetailContent2)
    {
        emit Check_Alert_Details(ui->m_labelDetailEvent2->text().trimmed().toInt());
    }
}

void WaringInfo::on_buttonFirst_clicked()
{
    /*待完成*/
    eMapper->toFirst();
}

void WaringInfo::on_buttonPrev_clicked()
{
    /*待完成*/
    int currentIndex = eMapper->currentIndex();
    if( currentIndex >= 3 )
    {
        currentIndex = currentIndex - 3;
        eMapper->setCurrentIndex(currentIndex);
    }
}

void WaringInfo::on_buttonNext_clicked()
{
    /*待完成*/
    int currentIndex = eMapper->currentIndex();
    currentIndex = currentIndex + 3;
    eMapper->setCurrentIndex(currentIndex);

}

void WaringInfo::on_buttonBack_clicked()
{
    /*待完成*/
    stackLayout->setCurrentIndex(SystemSetting4Page);
}

WaringInfo::~WaringInfo()
{
    delete ui;
}
